/**
 * 资金预算管理初始化
 */
var CapitalBudget = {
    id: "CapitalBudgetTable",	//表格id
     seItem: null,		//选中的条目
    table: null,
    //clickToSelect: false,
     layerIndex: -1
};

/**
 * 初始化表格的列
 */
CapitalBudget.initColumn = function () {
    return [


        [
			{field: 'selectItem', radio: true,visible: false,colspan: 1,rowspan: 2,width:'36px'},
            {
                field: 'number',
                title: "序号",
                valign:"middle",
                align:"center",
                colspan: 1,
                rowspan: 2
            },
            {
                field: 'project',
                title: "项目",
                halign:"center",
                valign:"middle",
                align:"left",
                colspan: 1,
                rowspan: 2
            },

            {
                title: "本期预计收入金额",
                valign:"middle",
                align:"center",
                colspan: 2,
                rowspan: 1
            },
            {
                title: "本期预计支出金额",
                valign:"middle",
                align:"center",
                colspan: 2,
                rowspan: 1
            },
            {
                field: 'remarks',
                title: "备注",
                valign:"middle",
                align:"center",
                colspan: 1,
                rowspan: 2
            },
            {
                field: 'revDate',
                title: "报表日期",
                valign:"middle",
                align:"center",
                colspan: 1,
                rowspan: 2
            },
        ],
        [
            {
                field: 'incomePS',
                title: 'PISO',
                halign:"center",
                valign:"middle",
                align:"right"
            },
            {
                field: 'incomeRMB',
                title: '折合人民币',
                halign:"center",
                valign:"middle",
                align:"right"
            },
            {
                field: 'expenditurePS',
                title: 'PISO',
                halign:"center",
                valign:"middle",
                align:"right"
            },
            {
                field: 'expenditureRMB',
                title: '折合人民币',
                halign:"center",
                valign:"middle",
                align:"right"
            }
        ]



    ];
};

/**
 * 检查是否选中
 */
CapitalBudget.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CapitalBudget.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加资金预算
 */
CapitalBudget.openAddCapitalBudget = function () {
    var index = layer.open({
        type: 2,
        title: '添加资金预算',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/capitalBudget/capitalBudget_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看资金预算详情
 */
CapitalBudget.openCapitalBudgetDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '资金预算详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/capitalBudget/capitalBudget_update/' + CapitalBudget.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除资金预算
 */
CapitalBudget.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/capitalBudget/delete", function (data) {
            Feng.success("删除成功!");
            CapitalBudget.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("capitalBudgetId",this.seItem.id);
        ajax.start();
    }
};

CapitalBudget.checkPayTime = function(){
    var revDate =  $("#revDate").val();
    if (revDate==null||revDate==""){
        Feng.info("请选择要导出的工资表时间");
        return false;
    } else {
        return true;
    }
};


/**
 * 导出
 */
CapitalBudget.exportCapitalBudget = function () {
    if (this.checkPayTime()) {
        window.location.href = "/capitalBudget/load?revDate=" + $("#revDate").val();
    }


};

/**
 * 导出
 */
CapitalBudget.exportModel = function () {
        window.location.href = "/capitalBudget/download";

};


/**
 * 打开文件上传选择框
 */
CapitalBudget.upLoadFile = function(){
    var index = layer.open({
        type:2,
        title:'请选择要上传的文件',
        fix:false,
        maxmin:true,
        content: Feng.ctxPath + '/capitalBudget/capitalBudget_upload'

    });
    this.layerIndex = index;
}


/**
 * 查询资金预算列表
 */
CapitalBudget.search = function () {
    var queryData = {};
    queryData['revDate'] = $("#revDate").val();
    queryData['platform'] = $("#platform").val();
    CapitalBudget.table.refresh({query: queryData});
    $("#CapitalBudgetTableSUM").remove();
    $("#CapitalBudgetTable").after(  tatol()   )


};

var tatol= function(){
    var queryData = {};
    queryData['revDate'] = $("#revDate").val();
    queryData['platform'] = $("#platform").val();
    var incometatol = 0.0;
    var outcometatol = 0.0;
    var tatol = 0.0;
    var ajax = new $ax(Feng.ctxPath + "/capitalBudget/tatol", function (result) {
        incometatol=result.incometatol;
        outcometatol=result.outcometatol;
        tatol=result.tatol;
    }, function (result) {
        Feng.error("查询失败!" + result.responseJSON.message + "!");
    });
    ajax.setData(queryData);
    ajax.start();


    var rowTem = '<table id="CapitalBudgetTableSUM" data-mobile-responsive="true" data-click-to-select="true" class="table table-hover table-striped" style="margin-top: -0px;color: #2A2E36;font-size: 14px;"><tbody id="CapitalBudgetTableSUM" >'+
        '<tr style="background-color: #e6f5c1"> <td  style="text-align: center; vertical-align: middle; width: 400px; " >总计</td> <td style="text-align: center; vertical-align: middle; ">收入总计</td>'+
        '<td style="text-align: center; vertical-align: middle; ">'+incometatol+'</td> <td style="text-align: center; vertical-align: middle; ">支出总计</td> <td style="text-align: center; vertical-align: middle; ">'+outcometatol+'</td>'+
        '<td style="text-align: center; vertical-align: middle; width: 400px "></td></tr>'+
        '<tr style="background-color: #e6f5c1"> <td  style="text-align: center; vertical-align: middle;" >本期资金余缺</td>  <td colspan="2" style="text-align: center; vertical-align: middle; ">'+tatol+'</td>'+
        '<td colspan="2" style="text-align: center; vertical-align: middle; "> </td> <td style="text-align: center; vertical-align: middle; "> </td> </tr></tbody></table>';
    return rowTem;
};

$(function () {
    var defaultColunms = CapitalBudget.initColumn();
    var table = new BSTable(CapitalBudget.id, "/capitalBudget/list", defaultColunms,CapitalBudget);
	table.height=750;
	table.pagination=false;
    table.setPaginationType("client");
    table.setSize(50);
	table.setShowColumns(false);
	table.setShowRefresh(false);
    CapitalBudget.table = table.init();
    $("#CapitalBudgetTable").after(tatol());
   //添加表头
	$(".fixed-table-toolbar").after("<div style='text-align: center;vertical-align: middle;font-size: 22px;color: #6979d7;font-weight: bold;'> 资金收支预算表</div>");
});
